﻿#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;
/// <summary>
/// /////////////////////////////////////////////////////////
/// </summary>
class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int n = nums.size();
        int len = 0;
        for (int left = 0, right = 0; left < n - 1; left++)
        {
            right = left;
            int zero = 0;
            while (zero <= k)
            {
                if (nums[right] == 1)
                {
                    right++;
                }
                else
                {
                    zero++;
                    right++;
                }
                len = right - 1 - left + zero;
            }
        }
        return len;
    }
};


/// <summary>
/// //////////////////////////////////////////////////////////////
/// </summary>
class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int n = nums.size();
        int len = 0;
        for (int left = 0, right = 0; left < n; left++)
        {
            int zero = 0;
            for (right = left + 1; right < n; right++)
            {
                if (nums[right] == 1)
                {
                    continue;
                }
                else
                {
                    zero++;
                }
                while (zero > k)
                {
                    zero--;
                    left++;
                }
                len = right - left + 1;
            }
        }
        return len;
    }
};

//////////////////////////////////////////////////////////////////////////

//此暴力枚举解法有问题
class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int n = nums.size();
        int len = 0;
        for (int left = 0, right = 0; left < n - 1; left++)
        {
            right = left;
            int zero = 0;
            while (zero <= k)
            {
                if (nums[right] == 1)
                {
                    right++;
                }
                else
                {
                    zero++;
                    right++;
                }
                len = max(len, right - left - 1);
            }
        }
        return len;
    }
};


class Solution
{
public:
    int longestOnes(vector<int>& nums, int k)
    {
        int ret = 0;
        for (int left = 0, right = 0, zero = 0; right < nums.size(); right++)
        {
            if (nums[right] == 0) zero++; // 进窗⼝ 
            while (zero > k) // 判断 
                if (nums[left++] == 0) zero--; // 出窗⼝ 
            ret = max(ret, right - left + 1); // 更新结果 
        }
        return ret;
    }
};

/////////////////////////////////////////////////////////
class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int n = nums.size();
        int len = 0;
        for (int left = 0, right = 0, zero = 0; right < n; right++) {
            
            if (nums[right] == 0) {
                zero++;
            }
            while (zero > k) {
                if (nums[left++] == 0)
                    zero--;
            }
            len = max(len, right - left + 1);
        }
        return len;
    }
};
/////////////////////////////////////////
class Solution
{
public:
    int minOperations(vector<int>& nums, int x)
    {
        int sum = 0;
        for (int a : nums) sum += a;
        int target = sum - x;
        // 细节问题 
        if (target < 0) return -1;
        int ret = -1;
        for (int left = 0, right = 0, tmp = 0; right < nums.size(); right++)
        {
            tmp += nums[right]; // 进窗⼝ 
            while (tmp > target) // 判断 
                tmp -= nums[left++]; // 出窗⼝ 
            if (tmp == target) // 更新结果 
                ret = max(ret, right - left + 1);
        }
        if (ret == -1) return ret;
        else return nums.size() - ret;
    }
};

